package com.have.blog.xo.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.commons.feign.PictureFeignClient;
import com.have.blog.model.api.CommonPage;
import com.have.blog.model.mapper.TResourceSortMapper;
import com.have.blog.model.mapper.TStudyVideoMapper;
import com.have.blog.model.models.TResourceSort;
import com.have.blog.model.models.TResourceSortExample;
import com.have.blog.model.models.TStudyVideoExample;
import com.have.blog.utils.MyStringUtils;
import com.have.blog.utils.ResultUtils;
import com.have.blog.xo.constant.MessageConstant;
import com.have.blog.xo.constant.SysConstant;
import com.have.blog.xo.service.admin.ResourceCategoryService;
import com.have.blog.xo.uitls.WebUtil;
import com.have.blog.xo.vo.ResourceCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class ResourceCategoryServiceImpl implements ResourceCategoryService {
    @Autowired
    private TResourceSortMapper resourceSortMapper;
    @Autowired
    private TStudyVideoMapper videoMapper;
    @Autowired
    private PictureFeignClient pictureFeignClient;
    @Autowired
    private WebUtil webUtil;

    @Override
    public String stickResourceSort(ResourceCategoryVo resourceSortVO) {
        PageHelper.startPage(1, 1);
        TResourceSortExample example = new TResourceSortExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("sort desc");
        List<TResourceSort> list = resourceSortMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return ResultUtils.successWithMessage(MessageConstant.OPERATION_SUCCESS);
        }
        TResourceSort resourceSort = resourceSortMapper.selectByPrimaryKey(resourceSortVO.getUid());
        TResourceSort maxSort = list.get(0);
        if (StringUtils.isEmpty(maxSort.getUid())) {
            return ResultUtils.errorWithMessage(MessageConstant.PARAM_INCORRECT);
        }
        if (maxSort.getUid().equals(resourceSort.getUid())) {
            return ResultUtils.errorWithMessage(MessageConstant.THIS_SORT_IS_TOP);
        }
        resourceSort.setSort(maxSort.getSort() + 1);
        resourceSort.setUpdateTime(new Date());
        int result = resourceSortMapper.updateByPrimaryKeySelective(resourceSort);
        if (result < 1) {
            return ResultUtils.errorWithMessage(MessageConstant.OPERATION_FAIL);
        }
        return ResultUtils.successWithMessage(MessageConstant.OPERATION_SUCCESS);
    }

    @Override
    public String deleteBatchResourceSort(List<ResourceCategoryVo> resourceSortVOList) {
        List<String> uids = new ArrayList<>();
        resourceSortVOList.forEach(item -> {
            uids.add(item.getUid());
        });

        TStudyVideoExample videoExample = new TStudyVideoExample();
        videoExample.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andResourceSortUidIn(uids);
        long count = videoMapper.countByExample(videoExample);
        if (count > 0) {
            return ResultUtils.errorWithMessage(MessageConstant.RESOURCE_UNDER_THIS_SORT);
        }

        TResourceSort template = new TResourceSort();
        template.setStatus(BlogStatus.DISABLE.getStatus());
        template.setUpdateTime(new Date());

        TResourceSortExample example = new TResourceSortExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andUidIn(uids);
        int result = resourceSortMapper.updateByExampleSelective(template, example);
        if (result < 1) {
            return ResultUtils.errorWithMessage(MessageConstant.DELETE_FAIL);
        }
        return ResultUtils.successWithMessage(MessageConstant.DELETE_SUCCESS);
    }

    @Override
    public String editResourceSort(ResourceCategoryVo resourceSortVO) {
        TResourceSort resourceSort = resourceSortMapper.selectByPrimaryKey(resourceSortVO.getUid());

        if (!resourceSort.getSortName().equals(resourceSortVO.getSortName())) {
            TResourceSortExample example = new TResourceSortExample();
            example.createCriteria()
                    .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                    .andSortNameEqualTo(resourceSortVO.getSortName());
            long count = resourceSortMapper.countByExample(example);
            if (count > 0) {
                return ResultUtils.errorWithMessage(MessageConstant.ENTITY_EXIST);
            }
        }
        resourceSort.setSortName(resourceSortVO.getSortName());
        resourceSort.setContent(resourceSortVO.getContent());
        resourceSort.setFileUid(resourceSortVO.getFileUid());
        resourceSort.setSort(resourceSortVO.getSort());
        resourceSort.setUpdateTime(new Date());
        int result = resourceSortMapper.updateByPrimaryKeySelective(resourceSort);
        if (result < 1) {
            return ResultUtils.errorWithMessage(MessageConstant.UPDATE_FAIL);
        }
        return ResultUtils.successWithMessage(MessageConstant.UPDATE_SUCCESS);
    }

    @Override
    public String addResourceSort(ResourceCategoryVo resourceSortVO) {
        TResourceSortExample example = new TResourceSortExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andSortNameEqualTo(resourceSortVO.getSortName());
        long count = resourceSortMapper.countByExample(example);
        if (count > 0) {
            return ResultUtils.errorWithMessage(MessageConstant.ENTITY_EXIST);
        }

        TResourceSort resourceSort = new TResourceSort();
        resourceSort.setUid(MyStringUtils.getUUID());
        resourceSort.setSortName(resourceSortVO.getSortName());
        resourceSort.setContent(resourceSortVO.getContent());
        resourceSort.setFileUid(resourceSortVO.getFileUid());
        resourceSort.setSort(resourceSortVO.getSort());
        resourceSort.setStatus(BlogStatus.ENABLE.getStatus());
        resourceSort.setUpdateTime(new Date());
        resourceSort.setCreateTime(new Date());
        int result = resourceSortMapper.insert(resourceSort);
        if (result < 1) {
            return ResultUtils.errorWithMessage(MessageConstant.INSERT_FAIL);
        }
        return ResultUtils.successWithMessage(MessageConstant.INSERT_SUCCESS);
    }

    @Override
    public String getPageList(ResourceCategoryVo resourceSortVO) {
        PageHelper.startPage(resourceSortVO.getCurrentPage(), resourceSortVO.getPageSize());
        TResourceSortExample example = new TResourceSortExample();
        TResourceSortExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(resourceSortVO.getKeyword()) && !StringUtils.isEmpty(resourceSortVO.getKeyword().trim())) {
            criteria.andSortNameLike("%" + resourceSortVO.getKeyword() + "%");
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("sort desc");
        List<TResourceSort> list = resourceSortMapper.selectByExample(example);

        StringBuilder sb = new StringBuilder();
        list.forEach(item -> {
            if (!StringUtils.isEmpty(item.getFileUid())) {
                sb.append(item.getFileUid() + SysConstant.FILE_SEGMENTATION);
            }
        });
        String pictureResult = null;
        Map<String, String> pictureMap = new HashMap<>();

        if (!StringUtils.isEmpty(sb.toString())) {
            pictureResult = this.pictureFeignClient.getPicture(sb.toString(), SysConstant.FILE_SEGMENTATION);
        }
        List<Map<String, Object>> picList = webUtil.getPictureMap(pictureResult);

        picList.forEach(item -> {
            pictureMap.put(item.get(SysConstant.UID).toString(), item.get(SysConstant.URL).toString());
        });

        for (TResourceSort item : list) {
            //获取图片
            if (!StringUtils.isEmpty(item.getFileUid())) {
                List<String> pictureUidsTemp = MyStringUtils.changeStringToStringList(item.getFileUid(), SysConstant.FILE_SEGMENTATION);
                List<String> pictureListTemp = new ArrayList<>();
                pictureUidsTemp.forEach(picture -> {
                    pictureListTemp.add(pictureMap.get(picture));
                });
                item.setPhotoList(pictureListTemp);
            }
        }
        return ResultUtils.successWithData(CommonPage.restPage(list));
    }
}
